package com.fivezerotwo.springbootserver.controller;

import com.fivezerotwo.springbootserver.bll.UserBll;
import com.fivezerotwo.springbootserver.common.ReturnModel.Result;
import com.fivezerotwo.springbootserver.common.ReturnModel.ReturnModel;
import com.fivezerotwo.springbootserver.dto.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户控制类
 * Created by qiqi on 2018/7/10 20:07
 */
@Slf4j
@RestController
@RequestMapping(value = "/user", method = {RequestMethod.GET, RequestMethod.POST})
@Api(tags = "用户相关接口")
public class UserController {

    //private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserBll userBll;

    /**
     * 获取用户列表
     * @return
     */
    @ApiOperation(value = "获取用户列表")
    @RequestMapping("list")
    public Result getUserList() {
        try {
            List<User> users = userBll.getUserList();
            log.info("获取用户列表: {}", users);
            return ReturnModel.successPageRequest(users);
        } catch (Exception e) {
            log.info("获取用户列表异常：{}", e.getMessage());
            return ReturnModel.serverErroRequest(e.getMessage());
        }
    }

    /**
     * 添加用户
     * @param name          必传     用户姓名
     * @param password      必传      用户密码
     * @param role          必传      用户角色  0-学生 1-老师
     * @return
     */
    @ApiOperation(value = "添加用户")
    @RequestMapping("/add")
    public Result addUser(
            @ApiParam(value = "用户姓名", required = true) @RequestParam(value = "name")String name,
            @ApiParam(value = "用户密码", required = true) @RequestParam(value = "password")String password,
            @ApiParam(value = "用户角色") @RequestParam(value = "role", required = false, defaultValue = "0")int role
    ) {
        try {
            boolean ret = userBll.addUser(name, role, password);
            log.info("添加用户成功：{}",ret);
            return ReturnModel.successRequest(ret);
        } catch (Exception e) {
            log.info("添加用户异常：{}", e);
            return ReturnModel.serverErroRequest(e.getMessage());
        }
    }
}
